package com.service.ningdong.impl;

import com.dao.*;

import com.entity.*;
import com.entity.BuildingAreaQuery;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CountServiceImpl {

    @Autowired
    private BuildingareaDao buildingareaDao;
    @Autowired
    private ZoneDao zoneDao;
    @Autowired
    private HouseDao houseDao;
    @Autowired
    private BuildingDao buildingDao;
    @Autowired
    private RoomtypeDao roomtypeDao;



    public List<BuildingAreaQuery> buildingacreageCount(Date startTime, Date endTime) {
        List<BuildingAreaQuery> buildingAreaQueries = new ArrayList<>();

        List<Zone> zoneList = zoneDao.selectByExample(null);

        for(Zone zone:zoneList){
            BuildingAreaQuery buildingAreaQuery = new BuildingAreaQuery();
            int acreage=0;
            BuildingareaExample buildingareaExample = new BuildingareaExample();
            buildingareaExample.createCriteria().andZidEqualTo(zone.getZid());
            List<Buildingarea> buildingareaList = buildingareaDao.selectByExample(buildingareaExample);

            for(Buildingarea buildingarea:buildingareaList){


                HouseExample houseExample = new HouseExample();
                houseExample.createCriteria().andBidEqualTo(buildingarea.getBid());
                List<House> houseList = houseDao.selectByExample(houseExample);


                for(House house:houseList){

                    BuildingExample buildingExample = new BuildingExample();
                    buildingExample.createCriteria().andHidEqualTo(house.getHid());
                    List<Building> buildingList = buildingDao.selectByExample(buildingExample);

                    for(Building building:buildingList){
                        Integer totalarea = building.getTotalarea();
                        acreage += totalarea;
                    }
                    HouseQuery houseQuery = new HouseQuery();

                    houseQuery.setBuildingList(buildingList);
                    houseQuery.setHouse(house);
                }
            }
            buildingAreaQuery.setZone(zone);
            buildingAreaQuery.setAcreage(acreage);
            buildingAreaQueries.add(buildingAreaQuery);

        }
        return buildingAreaQueries;
    }


    public List<BuildingAreaQuery> buildingareaCount(Date startTime, Date endTime) {
        List<BuildingAreaQuery> buildingAreaQueries = new ArrayList<>();
        List<Buildingarea> buildingareaList = buildingareaDao.selectByExample(null);
        System.out.println("uuuuuuuuu"+buildingareaList);
        for(Buildingarea buildingarea:buildingareaList){
            BuildingAreaQuery buildingAreaQuery = new BuildingAreaQuery();
            int housenum = 0;
            int buildingnum =0;
            HouseExample houseExample = new HouseExample();
            houseExample.createCriteria().andBidEqualTo(buildingarea.getBid());
            List<House> houseList = houseDao.selectByExample(houseExample);
            for(House house:houseList){
                housenum+=1;
                BuildingExample buildingExample = new BuildingExample();
                buildingExample.createCriteria().andHidEqualTo(house.getHid());
                List<Building> buildingList = buildingDao.selectByExample(buildingExample);
                for(Building building:buildingList){
                    buildingnum+=1;
                }
            }
            buildingAreaQuery.setBuildingarea(buildingarea);
            buildingAreaQuery.setHousenum(housenum);
            buildingAreaQuery.setBuildingnum(buildingnum);
            buildingAreaQueries.add(buildingAreaQuery);
        }


        return buildingAreaQueries;
    }

    public List<HousePropertyCount> houseProperty(Date startTime, Date endTime) {

        List<HousePropertyCount> housePropertyCountList = new ArrayList<>();
        List<Roomtype> roomtypeList = roomtypeDao.selectByExample(null);
        for(Roomtype roomtype:roomtypeList){
            HousePropertyCount housePropertyCount = new HousePropertyCount();
            int count=0;
            int acreage=0;
            HouseExample houseExample = new HouseExample();
            houseExample.createCriteria().andRidEqualTo(roomtype.getRid());
            List<House> houseList = houseDao.selectByExample(houseExample);
            for(House house:houseList){
                BuildingExample buildingExample = new BuildingExample();
                buildingExample.createCriteria().andHidEqualTo(house.getHid());
                List<Building> buildingList = buildingDao.selectByExample(buildingExample);
                for(Building building:buildingList){
                    count+=1;
                    acreage += building.getTotalarea();

                }
            }
            housePropertyCount.setRoomtype(roomtype);
            housePropertyCount.setCount(count);
            housePropertyCount.setAcreage(acreage);
            housePropertyCountList.add(housePropertyCount);
        }
        return housePropertyCountList;

    }
}
